package com.vvlive.caibao.smartcity.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/1.
 * Dialog
 */
public class CustomDialog extends AlertDialog implements View.OnClickListener {

    private View layout;
    private TextView mTitle, mContent;
    private Button mPositive, mNegative;

    private OnDialogClickListener onDialogClickListener;

    private String mPositiveText = "确定", mNegativeText = "取消";
    private String mDialogTitle, mDialogContent;

    public CustomDialog(Context context) {
        this(context, 0);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        initLayout();
    }

    private void initLayout() {
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        layout = getLayoutInflater().inflate(R.layout.view_custom_dialog, null, false);
        mTitle = (TextView) layout.findViewById(R.id.dialog_title);
        mContent = (TextView) layout.findViewById(R.id.dialog_content);
        mPositive = (Button) layout.findViewById(R.id.dialog_positive);
        mNegative = (Button) layout.findViewById(R.id.dialog_negative);
        mPositive.setOnClickListener(this);
        mNegative.setOnClickListener(this);
        this.setView(layout);
    }

    public View getLayout() {
        return layout;
    }

    public void setLayout(View layout) {
        this.layout = layout;
    }

    public OnDialogClickListener getOnDialogClickListener() {
        return onDialogClickListener;
    }

    /**
     * setOnDialogClickListener
     *
     * @param onDialogClickListener
     * @param text text[0]: positive text, text[1]: negative text;
     */
    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener, String...text) {
        this.onDialogClickListener = onDialogClickListener;
        if(text.length > 0) {
           this.mPositiveText = text[0];
           this.mNegativeText = text[1];
        }
    }

    public String getmDialogTitle() {
        return mDialogTitle;
    }

    public void setmDialogTitle(String mDialogTitle) {
        this.mDialogTitle = mDialogTitle;
    }

    public String getmDialogContent() {
        return mDialogContent;
    }

    public void setmDialogContent(String mDialogContent) {
        this.mDialogContent = mDialogContent;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_positive:
                if(onDialogClickListener != null){
                    onDialogClickListener.onPositive();
                }
                dismiss();
                break;
            case R.id.dialog_negative:
                if(onDialogClickListener != null){
                    onDialogClickListener.onNegative();
                }
                dismiss();
                break;
        }
    }

    public interface OnDialogClickListener{
        void onPositive();
        void onNegative();
    }

    @Override
    public void show() {

        if (mDialogTitle == null) {
            mTitle.setVisibility(View.GONE);
        } else {
            mTitle.setText(mDialogTitle);
        }

        if (mDialogContent == null) {
            mContent.setVisibility(View.INVISIBLE);
        } else {
            mContent.setText(mDialogContent);
        }

        mPositive.setText(mPositiveText);
        mNegative.setText(mNegativeText);
        super.show();
        int width = getWindow().getWindowManager().getDefaultDisplay().getWidth();
        getWindow().setLayout(width - 50, ViewGroup.LayoutParams.WRAP_CONTENT);
//
    }
}
